package com.itheima.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.cargo.ContractDao;
import com.itheima.dao.cargo.ContractProductDao;
import com.itheima.dao.cargo.ExtCproductDao;
import com.itheima.domain.cargo.*;
import com.itheima.service.cargo.ContractProductService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ContractProductServiceImpl implements ContractProductService {

    @Autowired
    private ContractProductDao contractProductDao;

    @Autowired
    private ContractDao contractDao;

    @Autowired
    private ExtCproductDao extCproductDao;

    @Override
    public void save(ContractProduct contractProduct) {
        //查询当前货物对应合同信息
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());

        //==================================附件无影响=====================================//
        //==================================货物新增=======================================//
        //1 计算小计金额
        Double amount = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(amount);

        //2 执行新增
        contractProductDao.insertSelective(contractProduct);

        //==================================合同修改=======================================//
        //1 修改货物种数
        contract.setProNum(contract.getProNum() + 1);

        //2 修改总金额
        contract.setTotalAmount(contract.getTotalAmount() + amount);

        //3 执行修改
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //查询原来货物的小计金额
        ContractProduct contractProductOld = contractProductDao.selectByPrimaryKey(contractProduct.getId());
        Double amountOld = contractProductOld.getAmount();
        //查询当前货物对应合同信息
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());

        //==================================附件无影响=====================================//


        //==================================货物修改=======================================//
        //1 计算小计金额
        Double amount = contractProduct.getPrice() * contractProduct.getCnumber();
        contractProduct.setAmount(amount);

        //2 执行修改
        contractProductDao.updateByPrimaryKeySelective(contractProduct);

        //==================================合同修改=======================================//
        //1 总金额(合同) - 原来的货物小计金额(数据库)  +  现在的货物小计金额(计算)
        contract.setTotalAmount(contract.getTotalAmount() - amountOld + amount);

        //2 执行修改
        contractDao.updateByPrimaryKeySelective(contract);
    }

    //货物的id
    @Override
    public void delete(String id) {
        //查询当前货物对应的所有附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        extCproductExample.createCriteria().andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproducts = extCproductDao.selectByExample(extCproductExample);

        //查询当前货物的信息
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);

        //查询当前货物对应的合同信息
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());


        //==================================附件删除=====================================//
        //删除附件
        for (ExtCproduct extCproduct : extCproducts) {
            extCproductDao.deleteByPrimaryKey(extCproduct.getId());
        }

        //==================================货物删除=====================================//
        contractProductDao.deleteByPrimaryKey(id);


        //==================================合同修改=====================================//
        //1 货物种数
        contract.setProNum(contract.getProNum() - 1);

        //2 附件种数
        contract.setExtNum(contract.getExtNum() - extCproducts.size());

        //3 合同总金额
        Double amount = 0d;
        for (ExtCproduct extCproduct : extCproducts) {
            amount += extCproduct.getAmount();
        }
        amount += contractProduct.getAmount();
        contract.setTotalAmount(contract.getTotalAmount() - amount);

        //4 执行修改
        contractDao.updateByPrimaryKeySelective(contract);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public List<ContractProduct> findAll(ContractProductExample example) {
        return contractProductDao.selectByExample(example);
    }

    @Override
    public PageInfo findByPage(int pageNum, int pageSize, ContractProductExample example) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        return new PageInfo(list, 10);
    }

    @Override
    public void patchSave(List<ContractProduct> list) {
        for (ContractProduct contractProduct : list) {
            this.save(contractProduct);
        }
    }
}
